package com.lz.sports.core

import android.text.TextUtils
import com.lz.comm.SP
import com.lz.sports.LogUtils
import com.lz.sports.bean.CommonModel
import com.lz.sports.bean.VerifyModelDetail
import com.lz.sports.utils.jsonToBean
import com.lz.sports.utils.readAssetsText

//认证类型管理
object VerifyManager {

    private var verifyModelDetail: VerifyModelDetail

    init {
        val modelDetail = modelDetail() ?: throw IllegalStateException("VerifyModelDetail.txt has none models")
        verifyModelDetail = modelDetail
    }

    private fun modelDetail(): VerifyModelDetail? {
        val verifyModelStr = readAssetsText("VerifyModelDetail.txt")
        LogUtils.i("ver = $verifyModelStr")
        if (!TextUtils.isEmpty(verifyModelStr)) {
            val verifyModelDetail = verifyModelStr.jsonToBean<VerifyModelDetail>()
            if (verifyModelDetail != null && !verifyModelDetail.isInvalid()) {
                return verifyModelDetail
            }
        }
        return null
    }

    //检录方式列表
    fun inspectModels(): ArrayList<CommonModel> = verifyModelDetail.inspectModels!!

    //签到类型列表
    fun signInModels(): ArrayList<CommonModel> = verifyModelDetail.signInModels!!

    //查询类型列表
    fun queryModels(): ArrayList<CommonModel> = verifyModelDetail.queryModels!!

    //确认类型列表
    fun sureModels(): ArrayList<CommonModel> = verifyModelDetail.sureModels!!

    //检录模式
    fun inspectModel(): CommonModel {
        val inspectType = SP.getInt(K_CHECK_IN_MODE, v_checkInMode)
        val inspectModels = inspectModels()
        for (model in inspectModels) {
            if (model.type == inspectType) return model
        }
        val first = inspectModels[0]
        SP.setInt(K_CHECK_IN_MODE, first.type)
        return first
    }

    //签到模式
    fun signInModel(): CommonModel {
        val signInType = SP.getInt(K_SIGN_IN_MODE, v_signInMode)
        val signInModels = signInModels()
        for (model in signInModels) {
            if (model.type == signInType) return model
        }
        val first = signInModels[0]
        SP.setInt(K_SIGN_IN_MODE, first.type)
        return first
    }

    //查询模式
    fun queryModel(): CommonModel {
        val queryType = SP.getInt(K_SCORE_QUERY_MODEL, v_score_query_model)
        val queryModels = queryModels()
        for (model in queryModels) {
            if (model.type == queryType) return model
        }
        val first = queryModels[0]
        SP.setInt(K_SCORE_QUERY_MODEL, first.type)
        return first
    }

    //确认模式
    fun sureModel(): CommonModel {
        val sureType = SP.getInt(K_SCORE_SURE_MODEL, v_score_sure_model)
        val sureModels = sureModels()
        for (model in sureModels) {
            if (model.type == sureType) return model
        }
        val first = sureModels[0]
        SP.setInt(K_SCORE_SURE_MODEL, first.type)
        return first
    }
}